﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@inject NavigationManager NavigationManager
@namespace ThingsGateway.Admin.Blazor
@inherits BaseComponentBase
@using BlazorComponent;
@using Masa.Blazor;
<div class="d-flex align-center py-1">
    <MBreadcrumbs Routable @key="@(Guid.NewGuid())" Class="pa-0">
        <DividerContent>
            <MIcon Class="ma-0 pa-0">mdi-chevron-right</MIcon>
        </DividerContent>
        <ChildContent>
            <div style="white-space: nowrap !important;overflow: hidden !important; text-overflow: ellipsis !important;">
                <MBreadcrumbsItem Href="javascript:history.back(-1)">
                    <MIcon Size=20>mdi-arrow-left</MIcon>
                </MBreadcrumbsItem>
                @for (var i = 0; i < BreadcrumbItems.Count; i++)
                {
                    var item = BreadcrumbItems[i];
                    var isLast = i == BreadcrumbItems.Count - 1;
                    <MBreadcrumbsItem Href="@item.Href">
                        <span class="@(isLast ? "text-subtitle2" : "text-body2")">@item.Text</span>
                    </MBreadcrumbsItem>
                }
            </div>

        </ChildContent>
    </MBreadcrumbs>
</div>


@code {
    private List<BreadcrumbItem> BreadcrumbItems = new();

    /// <summary>
    /// 当前的菜单与单页列表
    /// </summary>
    [Parameter]
    public List<SysResource> SysResources { get; set; } = new();

    /// <inheritdoc/>
    public override void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
        base.Dispose();
    }

    /// <inheritdoc/>
    protected override void OnInitialized()
    {
        NavigationManager.LocationChanged += OnLocationChanged;
        BreadcrumbItems = GetBreadcrumbItems();
        base.OnInitialized();
    }
    private List<BreadcrumbItem> GetBreadcrumbItems()
    {
        var items = new List<BreadcrumbItem>();
        var currentNav = SysResources.FirstOrDefault(n => n.Component is not null && NavigationManager.Uri.Replace(NavigationManager.BaseUri, "/") == (n.Component));
        if (currentNav is not null)
        {
            if (currentNav.ParentId != 0)
            {
                var parentNav = SysResources.FirstOrDefault(n => n.Id == currentNav.ParentId);
                if (parentNav != null)
                    items.Add(new BreadcrumbItem { Text = parentNav.Title, Href = null });
            }

            items.Add(new BreadcrumbItem() { Text = currentNav.Title, Href = currentNav.Component });
            items.Last().Href = currentNav.Component;
        }

        return items;
    }

    private void OnLocationChanged(object sender, LocationChangedEventArgs e)
    {
        BreadcrumbItems = GetBreadcrumbItems();
        InvokeAsync(StateHasChanged);
    }
}